package com.plusone.action;

import java.util.ArrayList;
import java.util.List;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.Entity;
import com.plusone.model.Contact;
import com.plusone.model.Datastore;
import com.plusone.toolset.InputVerifier;
import com.plusone.toolset.Mailer;

public class ContactAction {

	private InputVerifier verifier = null;	

	/**
	 * 
	 * @param verifier
	 */
	public ContactAction(InputVerifier verifier) {
		this.verifier = verifier;
	}

	/**
	 * 
	 * @param contact
	 * @return
	 */
	public boolean IsValid(Contact contact) {
		if (verifier.isValid(contact.getDisplayName(), 50)
				&& verifier.isValid(contact.getEmailAddress(), 50)
				&& verifier.isValid(contact.getPurpose(), 100)) {
			if (verifier.isValid(contact.getDisplayName())
					&& verifier.isValid(contact.getEmailAddress())
					&& verifier.isValid(contact.getPurpose())) {
				return true;
			}		
			return false;
		}
		return false;
	}

	/**
	 * 
	 * @param contact
	 */
	public void Save(Contact contact) {
		final DatastoreService datastore = Datastore.get();
		
		Entity contactEntity = new Entity("Contact");
		contactEntity.setProperty("displayName",
				contact.getDisplayName());
		contactEntity.setProperty("emailAddress",
				contact.getEmailAddress());
		contactEntity.setProperty("purpose",
				contact.getPurpose());
		contactEntity.setProperty("ipAddress", contact.getIpaddress());
		datastore.put(contactEntity);
	}
	
	/**
	 * 
	 * @param contact
	 */
	public void sendMail(Contact contact) {
		List<String> recipients = new ArrayList<String>();
		recipients.add("opportunity@codeplus.in");
		Mailer.Send("admin@codeplus.in", "Codeplus Admin",
				recipients, contact.getDisplayName(), contact.toString());
	}
}
